<?php

include_once "../regsettings.php";



$conn = mysql_connect($_REQUEST['servername'], $_REQUEST['rlogin'], $_REQUEST['rpass']) or die(mysql_error());
mysql_select_db($_REQUEST['dbname'],$conn)  or die(mysql_error());

//mysql_query("CREATE DATABASE " . $_REQUEST['dbname']) ;

mysql_select_db($_REQUEST['dbname'],$conn)  or die(mysql_error());

//mysql_query("CREATE USER 'regtest'@ '%' IDENTIFIED BY 'regtest';");

//mysql_query("GRANT USAGE ON * . * TO 'regtest'@ '%' IDENTIFIED BY 'regtest' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;");

$query1="CREATE TABLE `" . REG_USER_TABLE . "` (
  `user_id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(45) NOT NULL COMMENT 'login name',
  `password` varchar(32) default NULL COMMENT 'main acocunt password',
  `firstname` varchar(45) default NULL COMMENT 'User first name',
  `lastname` varchar(45) default NULL COMMENT 'User Last name',
  `email` varchar(127) default NULL COMMENT 'email for account retrieval',
  `temp_pass` varchar(45) default NULL COMMENT 'temporary password for account retrieval',
  `temp_expire_date` datetime default '2008-01-01 00:00:00' COMMENT 'expiration date for temporary password',
  `question` varchar(255) default NULL COMMENT 'Challenge question',
  `answer` varchar(45) default NULL COMMENT 'Challenge Answer',
  `created_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `last_login_date` timestamp NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`user_id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
mysql_query($query1);

$query2="INSERT INTO `" . REG_USER_TABLE . "` (`user_id`,`username`,`password`,`firstname`,`lastname`,`email`,`temp_pass`,`temp_expire_date`,`question`,`answer`,`created_date`,`last_login_date`) VALUES 
 (1,'admin','bffc5260075497528fe6255ab7d91f71','DefaultAdmin','Default admin','admin@nowhere.com',NULL,'2008-01-01 00:00:00',NULL,NULL,'2009-05-09 20:39:41','0000-00-00 00:00:00'),
 (2, 'default', 'bffc5260075497528fe6255ab7d91f71', 'Default', 'User', 'nothing@nowhere.com', '', '2009-10-19 21:58:01', NULL, NULL, '2009-10-21 21:20:36', '2009-10-18 22:00:24');
";
mysql_query($query2);


$query5 = "CREATE TABLE IF NOT EXISTS `" . REG_PAGES_TABLE . "` (
  `page_name` varchar(255) NOT NULL,
  `shown` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`page_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
mysql_query($query5);

$query6 = "INSERT INTO `" . REG_PAGES_TABLE . "` (`page_name`, `shown`) VALUES
('all', 1);";
mysql_query($query6);



$query7 = "CREATE TABLE IF NOT EXISTS `" . REG_USERS_ROLES . "` (
  `user_id` int(11) NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`role_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
mysql_query($query7);

$query7 = "INSERT INTO `" . REG_USERS_ROLES . "` (`user_id`, `role_id`) VALUES ('1', '1'), ('2', '2');";
mysql_query($query7);

$query3="CREATE TABLE `" . REG_ROLES_PAGES . "` (
  `role_id` int(11) NOT NULL,
  `page_name` varchar(255) NOT NULL,
  `permission` varchar(45) NOT NULL default 'RW',
  PRIMARY KEY  (`role_id`,`page_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
mysql_query($query3);

$query3="INSERT INTO `" . REG_ROLES_PAGES . "` (`role_id`, `page_name`, `permission`) VALUES
(1, 'all', 'RW'),
(2, 'all', 'RW');";
mysql_query($query3);

$query4 = "CREATE TABLE IF NOT EXISTS `" . REG_ROLE_TABLE . "` (
  `role_id` int(11) NOT NULL AUTO_INCREMENT,
  `role_name` varchar(50) NOT NULL,
  `role_description` varchar(255) NOT NULL,
  PRIMARY KEY (`role_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
mysql_query($query4);


$query4 = "INSERT INTO `" . REG_ROLES_TABLE . "` (`role_id`, `role_name`, `role_description`) VALUES
(1, 'admin', 'default administrative user with all permissions'),
(2, 'default', 'default user for new users');";
mysql_query($query4);


header("Location: list_pages.php");
?>
